package br.com.inner.custom;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.commons.lang.StringUtils;

public class SimpleSelect extends SimpleTagSupport {

	@SuppressWarnings("unchecked")
	@Override
	public void doTag() throws JspException, IOException {
		
		JspWriter out = getJspContext().getOut();
		
		String select = montaSelect();
		out.println(select);
		 
	}
	
	@SuppressWarnings("unchecked")
	private String montaOptions() {
		
		StringBuffer sb = new StringBuffer();

		List list = null;
		//Ngc ngc = new Ngc();
		
		try {
			EntityManager em = JPAManager.getEntityManager();
			//list = ngc.getAll(Class.forName(classe));
			list = retornaLista(em);
						
			if (list != null && !list.isEmpty() && list.get(0) instanceof Comparable) {
				Collections.sort(list);
			}
			
			if (!StringUtils.isBlank(getSort())) {
				if ( (getSort().equalsIgnoreCase("true")) )
				{
					try {
						Collections.sort(list);
					}catch (Exception e) {
						System.err.println("Erro ao ordenar.");
					}
				}
			}
			
			sb.append("<option value=\"-1\">Selecione uma op&ccedil;&atilde;o</option>");
		
			for (Object obj : list) {
				String selected = "";
				if ((name != null) ){
				
					String vl = Reflex.getValue(obj, valueProperty);
					String vl2 = Reflex.getValue(name, valueProperty);
				
					if ( StringUtils.isNotBlank(vl) && StringUtils.isNotBlank(vl2) && vl.equals(vl2)){
						selected=" selected ";	
					}
					
				}
				
				//sb.append("<option ").append(selected).append(" value=\"").append(Reflex.getValue(obj, valueProperty)).append("\"> ").append(Reflex.getValue(obj, labelProperty)).append(" </option> ");
				sb.append("<option ");
				sb.append(selected);
				sb.append(" value=\"");
				String s1 = Reflex.getValue(obj, valueProperty); 
				sb.append(s1);
				sb.append("\"> ");
				String s2 = Reflex.getValue(obj, labelProperty); 
				sb.append(s2);
				sb.append(" </option> ");
			}
		} catch (CustomException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return sb.toString();
		
	}
	
	protected List retornaLista(EntityManager em){
		List list = new ArrayList();
		String sql  = "Select c from "+getClasse()+" c ";
		Query q = em.createQuery(sql);
		list = q.getResultList();
		return list;
	}
	
	protected String montaSelect(){
		
		StringBuffer sb = new StringBuffer();
		
		sb.append("<select name=\"").append(property).append("\" ");
		
		if (!StringUtils.isBlank(onchange)){
			sb.append(" onChange=\"").append(onchange).append("\" ");
		}
		
		if (!StringUtils.isBlank(cssClass)){
			sb.append(" class=\"").append(cssClass).append("\" ");
		}
		
		if (!StringUtils.isBlank(id)){
			sb.append(" id=\"").append(id).append("\" ");
		}

/*		if (StringUtils.isNotBlank(onblur)){
			sb.append(" onBlur=\"").append(onblur).append("\" ");
		}
*/
		sb.append(" > ");
		sb.append(montaOptions());
		sb.append(" </select> ");
		return sb.toString();

		
	}
	
	private String classe;
	private String property;
	private String labelProperty;
	private String valueProperty;
	private String name;
	private String onchange;
//	private String onblur;
	private String sort;
	private String id;
	private String cssClass;
	
	public String getId(){
		return id;
	}
	
	public void setId(String id){
		this.id = id;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getClasse() {
		return classe;
	}

	public void setClasse(String classe) {
		this.classe = classe;
	}
	
	public String getCssClass() {
		return cssClass;
	}
	
	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getLabelProperty() {
		return labelProperty;
	}

	public void setLabelProperty(String labelProperty) {
		this.labelProperty = labelProperty;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getValueProperty() {
		return valueProperty;
	}

	public void setValueProperty(String valueProperty) {
		this.valueProperty = valueProperty;
	}
	
	public static void main(String[] args) {
		
		SimpleSelect ss = new SimpleSelect();
		ss.setProperty("cdArea");
		ss.setOnchange("javascript:buscaEmpresas(this.value)");
		
		System.out.println(ss.montaSelect());
		
	}
}
